<?php namespace Admin\Controller;

use Admin\Model\Goods;

class GoodsController extends AuthController{
	protected $db;
	//构造函数
	public function __init()
	{
		$this->db = new Goods;
	}

    //动作
	public function index(){
		$data = $this->db->getAll(Q('get.cat_id'));
		View::with('data',$data)->make();
	}

	public function is_delete()
	{
		if (Q('goods_id')) {

			if($this->db->is_delete())
			{
				echo 0;
			}
			else
			{
				echo 1;
			}
		}
		else
		{
			$data = $this->db->getDelete();
			View::with('data',$data)->make();
		}

	}

	public function add()
	{
		if(IS_POST)
		{
			if($this->db->store())
			{
				// $this->goodstore();
				View::success('添加成功',U('index'));
			}
			else
			{
				View::error($this->db->getError());
			}
		}
		else
		{
			//分配栏目
			$cat = new \Admin\Model\Cat;
			$catData = $cat->getAll();
			// p($catData);
			View::with('catData',$catData);

			//商品类型
			$type = new \Admin\Model\ShopType;
			$shopType = $type->getAll();
			View::with('shopType',$shopType);

			//分配品牌数据
			$brand = new \Admin\Model\Brand;
			$brandData = $brand->getAll();
			View::with('brandData',$brandData);
			View::make();
		}
	}

	public function edit()
	{
		if(IS_POST)
		{
			if($this->db->edit())
			{
				View::success('修改成功',U('index'));
			}
			else
			{
				View::error($this->db->getError());
			}
		}
		else
		{
			//分配栏目
			$cat = new \Admin\Model\Cat;
			$catData = $cat->getAll();
			View::with('catData',$catData);

			//商品类型
			$type = new \Admin\Model\ShopType;
			$shopType = $type->getAll();
			View::with('shopType',$shopType);

			//分配品牌数据
			$brand = new \Admin\Model\Brand;
			$brandData = $brand->getAll();
			View::with('brandData',$brandData);

			$pics = Db::table('shop_pics')->where('goods_id',$_GET['goods_id'])->get();
			View::with('pics',$pics);

			//读取当前编辑商品的数据
			$field = $this->db->one($_GET['goods_id']);
			// p($field);exit;
			View::with('field',$field);
			View::make();
		}
	}

	public function getAttrList()
	{
		echo $this->db->getAttrList(Q('goods_id',''),$_GET['type_id']);
		exit;

	}

	public function goodstore()
	{
		$goodattr = new \Admin\Model\GoodsAttr;
		$goods_id = Q('get.goods_id');
		if (IS_POST) {

			$format = $goodattr->format_add();
			$data = $goodattr->getdatas();
			View::with('data',$data);
			$num = $goodattr->goodstore($goods_id);
			View::with('num',$num);
			View::make();
		}
		else{
			$data = $goodattr->getdatas();
			View::with('data',$data);
			$num = $goodattr->goodstore($goods_id);
			View::with('num',$num);
			View::make();
		}
	}

	public function editStore()
	{
		$format=array();

		$format[$_POST['name']] = $_POST['value'];

		$data = Db::table('shop_store')->where('store_id',$_POST['store_id'])->update($format);
		if($data)
		{
			View::ajax(array('code'=>0,'message'=>'修改成功'));
		}
		else
		{
			View::ajax(array('code'=>1,'message'=>'修改失败'));
		}
	}


	public function delStore()
	{

		$data = Db::table('shop_store')->where('store_id',$_POST['store_id'])->delete();
		if($data)
		{
			View::ajax(array('code'=>0,'message'=>'删除成功'));
		}
		else
		{
			View::ajax(array('code'=>1,'message'=>'删除失败'));
		}
	}



}



